http://members.aol.com/nickjc67/fswdscript.htm Scripting Page There was a time not so very long ago when interpreted languages were all the rage. But then along came compilers with they're superior speed and state-of-the-art technology. The lowly interpreter was scorned and became the ugly duckling of the computer world. But fashions change and just like flared trousers and platform shoes, the humble interpreter has made a come back in the form of scripting which has all of the flashy bits that compilers have (Object Orientation, speed, interfaces with other languages etc...) but has the ease of use of the beloved interpreter AND it works over the Internet. It has truly become a beautiful Swan. Well sort of. Scripting can be a bit of a beast with differing standards and differing behaviour. Scripts have also become just more than for the Internet but have spread offline as well principally as programs running on Intranet servers (ASP or JSP) or as stand alone programs (such as HTA). They can use both JavaScript and VBScript but are not cross platform (most require Microsoft operating systems). As usual the author cannot accept any responsibility for damage of any kind arising from the use, misuse or modification of these programs and assumes no responsibilty for the correctness of the documentation or the correct functioning of the scripts. A full system backup should be carried out to a suitable medium before attempting to use any of these programs and tested on non-critical systems prior to use on essential systems. Feel free to view the code and incorporate it in your own scripts. Enjoy. Windows Scripting Host These scripts won't work unless you have Windows 98/ME/2000/XP and have it activated (many viruses use WSH) or have downloaded the Scripting Host from the Microsoft Web Site and installed it. Phone Book A simple phone book program. Active Server Pages (ASP) These scripts won't work unless you have ASP (Active Server Pages) and the PWS (Windows Personal Web Server) installed (comes with Frontpage) or running NT Server and IIS (Internet Information Server). Phone Book A simple phone book program using an Access Database (supplied) HTML Applications (HTA's) These scripts won't work unless you have IE5/6. Unzip and click on the .HTA file in Explorer. Phone Book A simple phone book program using HTA. Back to previous page Written by Nick Cheesman. Last updated: 01/03/2003 Please eMail me at: nickjc67@aol.com '=============================================== WEBlications The humble BASIC programming language has seen many changes in it's relatively short life. Castigated for being interpreted rather than compiled, it has however spawned a generation (or two) of programmers in it's time (myself included). However, it has been somewhat absent since the advent of the Graphical User Interface although perfectly good languages could be purchased at a price. Is it too much to ask that a programming language be bundled with the Operating System? Well wait no longer because the time of the interpreted programming language is back with a vengence. Armed with Interrnet Explorer 5 and these humble pages, you too can produce quite reasonable programs that not only provide all the usual Windows widgets (text boxes, drop down menu's etc...) but web page functionality as well that can act just like ordinary programs. Hold on to your hats...another generation of programmers is about to be created... Introduction Find out what it's all about Tutorial #1 A gentle introduction to WEBlication programming Tutorial #2 Displaying messages and getting input Tutorial #3 Displaying messages and getting input Tutorial #4 Displaying messages and getting input Tutorial #5 Using HTML GUI widgets Tutorial #6 Reading and writing files Samples Further samples can be found on my Scripting page here. Please Email me if you need help at: nickjc67@aol.com '========================================= Introduction In the good old days (I'm definitely getting old) when RAM was measured in bytes rather than megabytes and processor speeds were measured in single figures, most operating systems came with a programming language. DOS had GWBASIC and QBasic, UNIX had it's shell scripting, most micros used BASIC and CP/M relied on assembler. This gave users access to a programming language without the need to spend money. Magazines could publish program listings secure in the knowledge that users had access to the relevent language because it was bundled with the Operating System. Then along came the GUI (Graphical User Interface) and suddenly the facility dried up. QBasic was still shipped with Windows but it wasn't updated to take advantage of the new GUI so nobody used it (even though it is still a great way to get into programming). This has lead to a fragmentation of the number of languages that users could be using (Java, VB etc...) and only available to those who paid good money for it (Java is free but a decent version complete with drag and drop IDE is not). I believe this has lead to most users not being able to write even the most rudimentary programs. It could be argued that such skills are not required and that the GUI does it all for them. Sadly, this is just not true. There are many tasks that benefit from programming skills which the GUI does not encourage. Then Microsoft came up with the Windows Scripting Host. It could be easily programmed in VBScript (a version of BASIC) using a text editor and it gave the user access to virtually all the facilities that could be found in full languages like VB. It is relatively slow because it is interpreted but I consider that an advantage as there is no need to mess around with compilers and has much in common with the UNIX shell script. However, it lacked one vital ingredient. It's GUI features were limited to message boxes and input boxes which limited it's appeal. It is possible to write small applications with it (see my scripting pages) but the user interface is very sparse. Then along came Internet Explorer 5 and with it the HTA (HTML Application) which allowed scripts to be hosted inside IE complete with GUI widgets but without the limitations imposed by the browser. HTA's look just like ordinary windows and can be run just like programs but they can also host HTML pages complete with graphics and all the active content that comes with it. These I have called WEBlications. Surprisingly, for a technology so useful, I have seen little mention of it in the computer press. But you'll find all you need to know in these pages. I will be offering tutorials and sample applications and an email service should you get stuck and need some help. You will however be well advised to buy a book on VBScript (Javascript can also be used but I will not be covering it...yet...) and must be prepared to spend some time experimenting with writing your own programs. Warning Even though you will be programming in the relatively protected environment of the browser, it is still possible to crash or lock the computer which may require a reboot. The author cannot accept any responsibility for damage of any kind arising from the use, misuse or modification of these programs and assumes no responsibilty for the correctness of the documentation or the correct functioning of the scripts. A full system backup should be carried out to a suitable medium before attempting to use any of these programs and tested on non-critical systems prior to use on essential systems Back to previous page Written by Nick Cheesman. Last updated: 01/03/2003 Please eMail me at: nickjc67@aol.com '======================================= Tutorial #1 To follow this tutorial, you will first need to read the WEBlication documentation on the WEBlication introduction page. You will also need access to a Windows PC running Internet Explorer 5.0 or above. It will be necessary to construct a basic template file to make creating new files so much easier. This file will look something like this:- You will notice that it is an HTML file despite the fact that a WEBlication is run locally and not on the Internet. Even though a WEBlication acts like an ordinary Windows program, it is actually still being run in a browser window albeit a specialised one. This allows us to use all of the active content and GUI widgets you associate with the web without actually having to run it over a connection or via a local web server. I'll go through the file a line at a time. It's an ordinary web page which always starts with this tag to tell the browser to treat it as command rather than just plain text. This is the important bit. If you leave out this tag then some things such as message boxes will refuse to work with the file named with an extension of .HTA but will work in an ordinary browser window with a .HTM extension. I'll go through all this again further down. Close the script section. Now the HTML proper can be specified. More later. Close the main body of the HTML and close the file itself. Here's that file in full again. Cut and paste this into a file and name it Template.HTA. Double click on it in Windows Explorer and check there are no errors and that a window is displayed. If you make a copy of it and name it template.htm and remove the tag:- then it will run in a standard browser window. The advantage of using .HTA is that no security messages will be displayed once we start adding code which we will do now. Make a copy of template.hta and rename it to Msgbox.HTA. Between the two code tags add:- msgbox "Hello" This will make the file look as follows:- Save it and run it. You should see a window containg a message box containing a greeting. Click OK. Note that the window remains. Pressing F5 will refresh it and run the program again. Click OK and close the window in the normal way. Congratulations. You have written your first WEBlication. One thing needs to be added for completeness and that is some comments about the program, who wrote it, the date and perhaps a description of what it does. Probably not required on such a small program as this but it is a good habit to get into. Take a look at this:- I have ommitted all the HTML stuff for clarity but it will need to be included when the file is created. There are several ways of showing comments one of which is the REM or Reminder statement. Really a hangover from previous versions of BASIC and not really used anymore. The other is an apostrophe as shown above. Comments are part of the code and so must be inside the script tags. They can also be added between lines of code to explain what is happening although excessive use of comments can slow the code down a bit - not a problem on new machines but could give one of your users a problem if they are using older kit. The program is now complete. If you want to know more then view the next tutorial from the main menu. Back to previous page Written by Nick Cheesman. Last updated: 01/03/2003 Please eMail me at: nickjc67@aol.com '========================================== Tutorial #2 Welcome to the second tutorial. Hope you got on alright with the first. No? Send me an email if there is something you don't understand and I'll try to help. Last time, I left you with a finished program and I ended by explaining about putting comments in your code. But wait, did you update your template file so that comments can be added more easily. No? This is what it should look like:- This is the file you should continue to use from now on so save it as template.hta. On with some new stuff. One of the first things you will want to do is display messages. The simplest way is to use a mesaage box or msgbox in VBScript speak. Here's it's simplets form:- MsgBox "Hello" To test this, cut and paste and place it between the script tags. Your program should look something like this:- Save as Msgbox.HTA (or any name you like provide it ends in .hta) and double click on it in Windows (or Winnt) Explorer and see what it does. You must click OK to close the box. Pressiing F5 (see last tutorial) and will bring it back. Click OK and close the window in the usual way. Simple isn't it? You can display any word or words up to a limit of 255 characters provided it is written bewtween the quotes. Punctuation can also be displayed again provided it is between the quotes. However, the MsgBox function is capable of some extra functions. From now on, I'll just be printing the code between the comments and the --> tag. I'll leave it up to you to paste the code between the tags and save it with a relevent name. Take alook at this. Option Explicit MsgBox "Hello World!", 64, "Hello" This replaces the code in the previous example. It displays a message box but this time with an information icon and the box has the title "Hello" rather than just VBScript as before. There are other variations which I'll be covering in later tutorials. Now we can communicate with the user but very often, the user needs to give the program some kind of input. This is where the InputBox function comes in. This is how it looks. Option Explicit DIM MyInput MyInput = InputBox("Enter anything you like") The DIM statement is telling VBScript that a variable containing a value is going to be used. No type is assigned because (unlike other versions of BASIC) it has only a single type called a Variant which can contain both numbers or strings or a mixture of both. This makes VBScript easier to use but can lead to bugs which can be difficult to track down particularly if a number is expected when letters have been input. A topic for another tutorial. This code produces a box which the user can type into. It has a message telling the user what kind of input is required. Once the text has been entered, the user can either click OK (or press the Return key) to pass the value to the program and closes the box or click Cancel which simply closes the box. This example passes a value back but nothing is actually done with it. However, the following is a more useful program. Option Explicit Dim MyInput MyInput = InputBox("Enter your name", "Name") MsgBox ("Hello " & MyInput) This time, the input box has a title and asks the user to enter his/her name. This is then passed to a message box using the variable MyInput which is then displayed along with a simple greeting. The & joins two strings together. Note the space after the word hello to make the display look better. That completes this second tutorial. We'll get into something a little meatier next time. Back to previous page Written by Nick Cheesman. Last updated: 01/03/2003 Please eMail me at: nickjc67@aol.com '======================================== Tutorial #3 In this tutorial, I'll show how the computer can appear to be a bit more intelligent, able to make decisions based on input and modify it's output. This is achieved using the IF statement. I won't be repeating the entire text file as in previous tutorials so you will need to read them first. Here then is a simple IF script:- Option Explicit Dim MyVar Dim MyInput MyInput = InputBox("Enter a name", "Name") IF MyInput = "nick" then MsgBox ("Hello " & MyInput) end if As before, two variables are created (or DIMensioned) ready for use. We use the InputBox as before to get input from the user and store it in MyInput. This is then used by the IF statement to decide wether or not to display the input. As it stands, this is not terribly useful as only the exact user input (in this case the word nick in lower case) will allow the message box to be displayed. It would be more useful if the program could point out to the user the kind of input required if it is wrong. This is where the else statement makes an appearance. Dim MyVar dim MyInput MyInput = InputBox("Enter a name", "Name") if MyInput = "nick" then MsgBox ("Hello " & MyInput) else MsgBox ("Incorrect name " & MyInput & ". Should be nick") end if Now a message will be displayed no matter what is input. Note that an error condition will still occur even if the word Nick or NIck (or any other upper/lower case mixture) is input. To get around this, use the command LCase. Replace the following line:- MyInput = InputBox("Enter a name", "Name") with MyInput = LCase(InputBox("Enter a name", "Name")) This will take the output from the InputBox function (in this case a name) and convert it to lower case. Note that the variable MyVar is changed and is thus displayed in lower case in the message box. So now it is possible to show you another MsgBox function which will prove useful in the future. It looks like this. Option Explicit Dim MyVar MyVar = MsgBox ("Hello World!", 65, "MsgBox Example") MsgBox MyVar When run, the message box will display the return value of the MsgBox function which in this case will either be 1 or 2 depending on the button pressed by the user. This can be used as follows. Option Explicit Dim MyVar MyVar = MsgBox ("Select either button", 65, "MsgBox Example") IF MyVar = 1 then MsgBox "OK Returns " & MyVar else MsgBox "Cancel Returns " & MyVar End If So now, it will be possible to ask the user wether to carry out some operation or not (like overwriting an existing file for example) without resorting to text input but instead using only a single mouse click. Have a play with these programs and see if you can add your own variations. The next tutorial will be a little more taxing than this one as we'll be investigating loops. Back to previous page Written by Nick Cheesman. Last updated: 01/03/2003 Please eMail me at: nickjc67@aol.com '====================================== Tutorial #4 This particular tutorial may need a little more concentration as we are to embark on the tortuous subject of loops. It took me many months to get my head around loops and mulitiple loops still give me a headache. Take a look at this. FOR Loops Option Explicit Dim i for i = 1 to 5 MsgBox ("Variable i is now set to " & i) next Remember to add this to your template file (see previous tutorials) before attempting to run it. The variable i is an index which is added to each time the loop goes round. Here's how it works. i is set to be the value 1 and a msgbox is displayed showing that. The next tells the computer to go back to the for is and add 1 to the value (the STEP command has been omitted for clarity) then go through the loop again except that it displays the number 2 in the message box and so on until the value of 5 is reached and the loop ends which in this case means the end of the program. The loop can be any set to run using any valid numbers although the bigger the second value, the longer the loop will run which may temprarily hang the browser. It could even be set to run from 5 to 1 if required and can skip values using the form for i = 1 to 6 STEP 2 so that in this case only every other value is used. Take a while to digest this if you are new to programming. Those of you familiar with other dialects of BASIC may be wondering why the next is not followed by the index as in next i This is surely much better programming practice and so it is. Unfortunately, VBScript does not recognise it and will generate an error. There is a great deal that is not present in VBScript and we will encounter some more in due course. We will of course continue to use good programming practice whenever possible. When you are sure you know what's happening with for loops then move on the the next. DO Loops Option Explicit Dim i i = 1 DO i = i + 1 MsgBox ("Variable i is now set to " & i) LOOP until i = 5 The DO LOOP differs in that some of the work involved must be done by the programmer, particularly when it comes to incrementing the index variable. Generally, this kind of loop would not be used for this kind of application as a FOR loop would obviously be better but I've used it so that you can compare the two pieces of code which give precisely the same results. So the variable i is set to 1 (VBScript would normally set it to zero but it is good practice to give it a value even if it is going to be zero). The loop starts and we add 1 to the index value (increment it). This means that it is actually now set to 2 thus the msgbox displays the value as being 2. Where you put the incrementing code (i = i + 1) is important. Remember, the computer will not do it for you as in the above example. The message box is displayed then the value of i is tetsed to see if it has reached 5. The loop will continue until this condition is met. Note that LOOP is a keyword and cannot be used as a variable name which some BASIC's use to simulate this kind of structure. The computer goes back to the DO (NOT the i = 1) and goes through the code again, adding 1 (could be more if required), displays the value of i and tests in case it has reached 5. Eventually it will and the loop will end. Be aware that if a mistake is made and the loop never ends then the program will lock up the PC and you will need to shut it down from Task Manager. Don't be afraid to experiment and make mistakes - you'll learn much more from your mistakes than from your successes. I think that's probably enough brain bending code for this session. Well continue next time with more loops. Back to previous page Written by Nick Cheesman. Last updated: 01/03/2003 Please eMail me at: nickjc67@aol.com '========================================== Tutorial #5 I've decided to cover loops another time and give you something a little more interesting. WEBlications can do much more than display with message boxes. They can use HTML to display text boxes, buttons and all the other things that Web pages can. We'll start with a simple Hello World program but using some HTML GUI widgets to make it all look a lot more interesting. Here's the script code first. Option Explicit Dim strName Sub check_name strName = txtName.value if strName <> "" then msgbox "Hello " & txtName.value else msgbox "Please enter a name" end if end sub Looks fairly innocuous except that on it's own it doesn't work. That's because it's expecting there to be a text field named txtName to be present. The subroutine itself needs to be called in response to a button being pressed. The HTML you need, which goes between the body tags in your template file, is as follows:- Please enter your name


I'll go throught the HTML first. Please enter your name

Some helpful text asking for a name to be entered. Then below that there is a button defined with the name btnGo. The bit to watch is the onclick which is an event triggered by the user clicking on the button. The event then triggers a call to the subroutine in the script called check_name. Just think about that for the moment. Even though this is HTML, it can contain a little bit of script in it's tags that can call a routine between the script tags.
Finally, we have the text box that the script above is expecting. It has been set to a size (width) of 22 characters although it can contain much more tahn 22 characters because it scrolls along. It is up to the script writer to ensure that the input string isn't to large and prompt the user to change it. More of that another time. That's the HTML. Now for the script. Option Explicit Ensure that all variables are created before use. Dim strName Set a variable to contain a string. Sub check_name Start of the subroutine called from the HTML event onclick. strName = txtName.value The string variable we created will now take the value of whatever is in the text box. Remember, the user should have put a name in the box then pressed the Go button which triggered this routine to start. if strName <> "" then But we can't trust the user not to have pressed Go before putting in a name so we see if the text box is still empty. msgbox "Hello " & txtName.value If it isn't (<> means not equal to) then show the name in a message box along with a nice peice of greeting text. else otherwise msgbox "Please enter a name" Ask the user nicely for a name to be entered. end if End the if statement. end sub Finish the routine. This gives a very brief insight into how HTML content can be displayed and controlled by a script. More next time. Back to previous page Written by Nick Cheesman. Last updated: 01/03/2003 Please eMail me at: nickjc67@aol.com '======================================== Tutorial #6 So who needs more boring loops. What you will want to do however is read and write to files. These will be stored on the local drive NOT on the Internet and can therefore be regarded as safe (or at least as safe as any other file created by an application). So as usual, the program looks like this and must be cut and pasted into your template file (see previous tutorials if you've lost it). dim a dim b dim fs dim filename filename="hello.txt" Set fs = CreateObject("Scripting.FileSystemObject") Set b = fs.CreateTextFile(Filename, True) b.Close msgbox "Created" When run, this will create a file called hello.txt in the same directory as the HTA which contains nothing at all. Here's how it works. First create a few temporary variables that will hold references to objects. dim a dim b dim fs Now we need a variable to hold a filename. dim Filename Filename="hello.txt" First, we create an object that knows how to deal with the file system. Don't worry too much about objects. You've doubtless come across the word before and been completely put off by the kind of language used to describe them. Forget all that. An object contains programs called methods that can do useful things for you and makes getting to the methods easier. That's it in a nutshell (the more knowledgeable amongst you will be tutting by now - I've simplified okay?). Set fs = CreateObject("Scripting.FileSystemObject") Now we have a reference to the filesystem object, one of the methods contained within it is to create a file. The next line will create the file with the filename we've used above. Set b = fs.CreateTextFile(Filename, True) And then we close the file because we don't intend to write anything to it. Yet. b.Close Nice message to the user just to confirm that something happened. msgbox "Created" The file having been created can now be written to which is what the next program does. Start a new template file and cut and paste this into it. Const ForAppending = 8 A const (short for constant) is a variable that cannot be changed whilst the program is running. It holds a number that tells the file system object that we want to add to the existiing file. We setup some variables as before. dim a dim fs dim Filename Filename="hello.txt" As before, we need a reference to the file system object. Set fs = CreateObject("Scripting.FileSystemObject") And now, we use the open text file method to open a file to be added to rather than overwritten. Set a = fs.OpenTextFile(Filename, ForAppending) And now we want to write something to it. The writeline method adds a carriage return and linefeed to the end. When this file is opened in Notepad then it will display each character on two seperate lines. a.WriteLine("a") a.WriteLine("b") Close the file because we've finished. Failure to close a file may result in the file remaining empty or becoming corrupted. a.close msgbox "Written" Running this application more than once will merely append the same letters to the file making it larger and larger. Running the creation program will create a new version that will be empty again. So now to read from the file you've created. Some variables and a reference to the filesystem as before. dim fs dim a dim retstring Set fs = CreateObject("Scripting.FileSystemObject") The file is opened. Set a = fs.OpenTextFile("hello.txt") Now comes a loop. The program will keep going through the file until it reaches the end displaying each line in a message box as it goes. The file is then closed as usual. Do While a.AtEndOfStream <> True retstring = a.ReadLine msgbox retstring Loop a.Close This time, provided the file has been created then this will read in the first line then the second then repeat until the end of the file has been reached. No attempt is made to store the characters as this involves the use of arrays which of course will be the subject of a future tutorial. In the meantime, try changing the programs to create different files and try complete words and sentences to see how this might be useful in creating a simple database program. If you're really impatitient then try the sample program which you can navigate to from the main Weblication page. Have fun. Back to previous page Written by Nick Cheesman. Last updated: 01/03/2003 Please eMail me at: nickjc67@aol.com '========================================